/*
 * Copyright (c) 2012 Andrea De Rinaldis <ozioso at ipignoli.dyndns.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dyndns.ipignoli.droidcruise.ui;

import org.dyndns.ipignoli.droidcruise.notifications.MyNotification;
import org.dyndns.ipignoli.droidcruise.routes.Route;
import org.dyndns.ipignoli.droidcruise.themes.Theme;
import android.widget.ImageView;


public class SpeedUI extends UpdatableUI{
  private ImageView[]    imgSpeed;
  private ImageView[]    imgAverageSpeed;
  private ImageView[]    imgSuggestedSpeed;
  private int            speedLimitPC;
  private Theme          theme;
  private MyNotification notification;

  public SpeedUI(ImageView[] imgSpeed, ImageView[] imgAverageSpeed,
      ImageView[] imgSuggestedSpeed, int speedLimitPC, Theme theme,
      MyNotification notification){
    super();
    this.imgSpeed = imgSpeed;
    this.imgAverageSpeed = imgAverageSpeed;
    this.imgSuggestedSpeed = imgSuggestedSpeed;
    this.speedLimitPC = speedLimitPC;
    this.theme = theme;
    this.notification = notification;
  }

  public void setSpeedLimitPC(int speedLimitPC){
    this.speedLimitPC = speedLimitPC;
  }

  public void setSpeed(double current, double average, Route route,
      boolean radarSet, boolean gpsOk){
    if(current < 3)
      current = 0;
    
    double speedLimit =
        route == null ? 0d : route.getLimit() + route.getLimit() * speedLimitPC
            / 100;

    long suggested =
        route == null ? -1l : Math.round(speedLimit + (speedLimit - average)
            * route.getDistance() / (route.getLength() - route.getDistance()));

    int speedLevel = Theme.LEVEL_WARNING;
    int averageSpeedLevel = Theme.LEVEL_WARNING;
    int suggestedSpeedLevel = Theme.LEVEL_WARNING;
    if(gpsOk){
      speedLevel = Theme.LEVEL_NULL;
      averageSpeedLevel = Theme.LEVEL_NULL;
      suggestedSpeedLevel = Theme.LEVEL_NULL;
      if(route != null){
        suggested = Math.max(0, suggested);
        speedLevel = current > suggested ? Theme.LEVEL_ALERT : Theme.LEVEL_OK;
        if(current > suggested || (radarSet && current > speedLimit)){
          speedLevel = Theme.LEVEL_ALERT;
          if(delayer.shouldNotify(notification, false))
            queueNotification(notification, false);
        }
        else{
          speedLevel = Theme.LEVEL_OK;
          if(delayer.shouldNotify(notification, true))
            queueNotification(notification, true);
        }
        averageSpeedLevel =
            average > speedLimit ? Theme.LEVEL_ALERT : Theme.LEVEL_OK;
        suggestedSpeedLevel =
            suggested < speedLimit ? Theme.LEVEL_ALERT : Theme.LEVEL_OK;
      }
      else if(delayer.shouldNotify(notification, true))
        queueNotification(notification, true);
    }

    theme.setValue(imgSpeed, Math.round(current), speedLevel);
    theme.setValue(imgAverageSpeed, Math.max(0, Math.round(average)),
        averageSpeedLevel);
    theme.setValue(imgSuggestedSpeed, suggested > speedLimit * 2 ? 1000l
        : suggested, suggestedSpeedLevel);
  }
}
